home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / spiderweb / tools / tie / character.web < prev    next >
Text File  |  1992-07-05  |  6KB  |  149 lines

  1. @* The character set.
  2. One of the main goals in the design of \.{WEB} has been to make it readily
  3. portable between a wide variety of computers. Yet \.{WEB} by its very
  4. nature must use a greater variety of characters than most computer
  5. programs deal with, and character encoding is one of the areas in which
  6. existing machines differ most widely from each other.
  7.  
  8. To resolve this problem, all input to \.{WEAVE} and \.{TANGLE} is converted
  9. to an internal seven-bit code that is essentially standard ASCII, the
  10. ``American Standard Code for Information Interchange.''  The conversion
  11. is done immediately when each character is read in. Conversely,
  12. characters are converted from ASCII to the user's external
  13. representation just before they are output.
  14.  
  15. Such an internal code can be accessed by users of \.{WEB} by means of
  16. constructions like \.{@@'A'}, which should be distinguished from
  17. \.{'A'}.  The former is transformed by
  18. \.{TANGLE} into an integer that is the internal code of \.A, but
  19. the latter, a |char| constant, is not touched by
  20. \.{WEB}, and will be interpreted by the \cee\ complier according to
  21. the machine's character set. (Actually, of course, it gets translated
  22. into \.{WEB}'s internal code just like any other character in the
  23. input file, but then it gets translated back at output time.)
  24. @^ASCII code@>
  25.  
  26. Here is a table of the standard visible ASCII codes (\.{ } stands for
  27. a blank space):
  28. $$\def\:{\char\count255\global\advance\count255 by 1}
  29. \count255='40
  30. \vbox{
  31. \hbox{\hbox to 40pt{\it\hfill0\/\hfill}%
  32. \hbox to 40pt{\it\hfill1\/\hfill}%
  33. \hbox to 40pt{\it\hfill2\/\hfill}%
  34. \hbox to 40pt{\it\hfill3\/\hfill}%
  35. \hbox to 40pt{\it\hfill4\/\hfill}%
  36. \hbox to 40pt{\it\hfill5\/\hfill}%
  37. \hbox to 40pt{\it\hfill6\/\hfill}%
  38. \hbox to 40pt{\it\hfill7\/\hfill}}
  39. \vskip 4pt
  40. \hrule
  41. \def\^{\vrule height 10.5pt depth 4.5pt}
  42. \halign{\hbox to 0pt{\hskip -24pt\O{#0}\hfill}&\^
  43. \hbox to 40pt{\tt\hfill#\hfill\^}&
  44. &\hbox to 40pt{\tt\hfill#\hfill\^}\cr
  45. 04&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  46. 05&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  47. 06&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  48. 07&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  49. 10&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  50. 11&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  51. 12&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  52. 13&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  53. 14&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  54. 15&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  55. 16&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
  56. 17&\:&\:&\:&\:&\:&\:&\:\cr}
  57. \hrule width 280pt}$$
  58.  
  59. We introduce new types to distinguish between the transliterated characters
  60. and the characters in the outside world.  Let all
  61. ``interesting'' values that a |char| variable may take lie between
  62. |first_text_char| and |last_text_char|; for the ASCII code we can
  63. take |first_text_char=0| and |last_text_char=0177|. We will tell \.{WEB}
  64. to convert all input characters in this range to its own code, and balk at
  65. characters outside the range.  We make two assumptions: 
  66. |first_text_char>=0| and |char| has room for at least eight bits.
  67. @^system dependencies@>
  68.  
  69. @d first_text_char = 0 /* lowest interesting value of a |char| */
  70. @d last_text_char = 0177 /* highest interesting value of a |char| */
  71.  
  72. @<Input and output types@>=
  73. typedef char ASCII; /* type of characters inside \.{WEB} */
  74. typedef char outer_char; /* type of characters outside \.{WEB} */
  75.  
  76. @ The \.{WEAVE} and \.{TANGLE} processors convert between ASCII code and
  77. the user's external character set by means of arrays |xord| and |xchr|
  78. that are analogous to PASCAL's |ord| and |chr| functions.
  79.  
  80. @<Globals...@>=
  81. ASCII xord[last_text_char]; /* specifies conversion of input characters */
  82. outer_char xchr[0200]; /* specifies conversion of output characters */
  83.  
  84. @ Every system supporting \cee\ must be able to read and write the 95
  85. visible characters of standard ASCII above (although not necessarily using the
  86. ASCII codes to represent them).  Conversely, these characters, plus
  87. the newline, are sufficient to write any \cee\ program.  Other
  88. characters are desirable mainly in strings, and they can be referred
  89. to by means of escape sequences like \.{'\t'}.
  90.  
  91. The basic implementation of \.{WEB}, then, only has to assign an
  92. |xord| to these 95 characters (newlines are swallowed by the reading
  93. routines).  The easiest way to do this is to assign the characters to
  94. their positions in |xchr| and then invert the correspondence:
  95.  
  96. @c
  97. common_init()
  98. {
  99.   strcpy(xchr,"                                 !\"#$%&'()*+,-./0123456789\
  100. :;<=>?@@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ");
  101.   @<System-dependent parts of character set@>;
  102.   @<Invert |xchr| to get |xord|@>;
  103. }
  104.  
  105. @ The following system-independent code makes the |xord| array contain
  106. a suitable inverse to the information in |xchr|.
  107.  
  108. @<Invert |xchr|...@>= {
  109.   int i; /* to invert the correspondence */
  110.   for (i=first_text_char; i<=last_text_char; i++) xord[i]='\040';
  111.   for (i=1; i<0177; i++) xord[xchr[i]]=i;
  112. }
  113.  
  114. @ Some \cee\ compilers accept an extended character
  115. set, so that one can type things like \.^^Z\ instead of \.{!=}.
  116. If that's the case in your system, you should change this module,
  117. assigning positions |01| to |037| in the most convenient way;
  118. for example, at MIT you can just say
  119. $$\hbox{|for (i=1; i<=037; i++) xchr[i]=i;|}$$
  120. since \.{WEB}'s character set is essentially identical to MIT's,
  121. even with respect to characters less than |040| (see the definitions
  122. below).  If, however, the changes do not conform with these
  123. definitions you should change the definitions as well.
  124. @^system dependencies@>
  125. @^notes to myself@>
  126.  
  127. @<System-dependent parts of character set@>= /* nothing needs to be done */
  128.  
  129. @
  130. @d text_char =  char /* the data type of characters in text files */
  131.  
  132.  
  133. @<Input and output types@>=
  134. typedef char ascii_code; /* ascii codes from 0 to 127 */
  135. typedef FILE *text_file;
  136.  
  137.  
  138. @ One of the \ASCII{} codes below 040 has been given a
  139. symbolic name in \.{TIE} because it is used with a special
  140. meaning.
  141.  
  142. @d tab_mark = '\t' /* \ASCII{} code used as tab-skip */
  143.  
  144.  
  145. @ When we initialize the |xord| array and the remaining
  146. parts of |xchr|, it will be convenient to make use of an
  147. index variable, |i|.
  148.  
  149.